package br.com.persist.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.persist.api.Command;
import br.com.persist.api.PersistentContext;
import br.com.persist.api.RuntimeExecutionException;
import br.com.persist.api.VolatileContext;

public class Function {

	private static final Logger logger = LoggerFactory
			.getLogger(Function.class);

	private String name;
	private List<Command> commands = new ArrayList<Command>();
	private List<String> parameterNames = new ArrayList<String>();

	public Function(String name) {
		super();
		this.name = name;
	}

	public List<Command> getCommands() {
		return Collections.unmodifiableList(commands);
	}

	public void addParameter(String parameterName) {
		this.parameterNames.add(parameterName);
	}
	
	public List<String> getParameterNames() {
		return Collections.unmodifiableList(parameterNames);
	}

	public Object execute(VolatileContext volatileContext,
			PersistentContext persistentContext)
			throws RuntimeExecutionException {
		logger.debug("Executing function {}", this.name);

		Object returnObject = null;
		for (Command command : commands) {
			returnObject = command.execute(volatileContext, persistentContext);
		}

		return returnObject;
	}

	public String getName() {
		return name;
	}
}
